﻿using IOP.SgrA;
using IOP.SgrA.SilkNet.Vulkan;
using Silk.NET.Vulkan;
using System;
using System.Collections.Generic;
using System.Text;

namespace VkSample47
{
    public class SecondaryRenderingMiddleware : IProductionLineMiddleware<SecondaryVulkanRenderGroup>
    {
        public void Invoke(SecondaryVulkanRenderGroup context, RenderingProductionLineDelegate<SecondaryVulkanRenderGroup> next)
        {

            var camera = context.Camera;
            var view = camera.ViewMatrix;
            var project = camera.ProjectionMatrix;

            CommandBufferInheritanceInfo info = new CommandBufferInheritanceInfo
            {
                Framebuffer = context.Framebuffer.RawHandle,
                OcclusionQueryEnable = false,
                RenderPass = context.RenderPass.RawHandle,
                Subpass = 0
            };
            context.SecondaryCommandBuffer.Reset();
            context.SecondaryCommandBuffer.Begin(CommandBufferUsageFlags.CommandBufferUsageOneTimeSubmitBit | 
                CommandBufferUsageFlags.CommandBufferUsageRenderPassContinueBit, info);
            foreach(var item in context.GetContexts())
            {
                item.SetViewMatrix(in view);
                item.SetProjectionMatrix(in project);
                context.ItemProductionLine(item);
            }
            context.SecondaryCommandBuffer.End();
        }
    }
}
